package com.sky.service.impl;

import com.sky.dto.GoodsSalesDTO;
import com.sky.entity.Orders;
import com.sky.mapper.OrderMapper;
import com.sky.mapper.UserMapper;
import com.sky.service.ReportService;
import com.sky.vo.OrderReportVO;
import com.sky.vo.SalesTop10ReportVO;
import com.sky.vo.TurnoverReportVO;
import com.sky.vo.UserReportVO;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Service
public class ReportServiceImpl implements ReportService {
    @Autowired
    private OrderMapper orderMapper;
    @Autowired
    private UserMapper userMapper;

    /**
     * 统计范围时间内的营业额
     * @param begin
     * @param end
     * @return
     */
    @Override
    public TurnoverReportVO getTurnoverStatistics(LocalDate begin, LocalDate end) {
        //存放所有天
        List<LocalDate> dateList =new ArrayList<>();
        dateList.add(begin);
        while(!begin.equals(end)){
            begin =begin.plusDays(1);
            dateList.add(begin);
        }
        //存放每天的营业额
        List<Double> turnoverList =new ArrayList<>();
        for (LocalDate date:dateList) {
            //获取当天的最小时间和最大时间
            LocalDateTime beginTime =LocalDateTime.of(date, LocalTime.MIN);
            LocalDateTime endTime =LocalDateTime.of(date,LocalTime.MAX);
            //查询date日期对应的营业额,营业额指的是：状态为已完成的订单金额合计
            //select sum(amount) where order_time>beginTime and order_time < endTime and status=5
            Map map =new HashMap();
            map.put("begin",beginTime);
            map.put("end",endTime);
            map.put("status", Orders.COMPLETED);
            Double turnover =orderMapper.sumByMap(map);
            if (turnover==null){
                turnover=0.0;
            }
            turnoverList.add(turnover);
        }
        return TurnoverReportVO.builder()
                .dateList(StringUtils.join(dateList,","))
                .turnoverList(StringUtils.join(turnoverList,","))
                .build();
        
    }

    /**
     * 用户统计
     * @param begin
     * @param end
     * @return
     */
    @Override
    public UserReportVO getUserStatistics(LocalDate begin, LocalDate end) {
        //存放所有天
        List<LocalDate> dateList = new ArrayList<>();
        dateList.add(begin);
        while(!begin.equals(end)){
            begin=begin.plusDays(1);
            dateList.add(begin);
        }
        //创建集合存放每天的用户数量和新增用户数量
        List<Integer> userList =new ArrayList<>();
        List<Integer> newUserList =new ArrayList<>();
        //获取小于范围内第一天的用户数量
        //select count(id) from user where create_time <begin
        LocalDateTime firstTime = LocalDateTime.of(begin,LocalTime.MIN);
        Map firstMap =new HashMap();
        firstMap.put("end",firstTime);
        int a = userMapper.getUserCount(firstMap);
        for (LocalDate date :dateList) {
            //获取当前的开始时间和结束时间
            LocalDateTime beginTime = LocalDateTime.of(date,LocalTime.MIN);
            LocalDateTime endTime =LocalDateTime.of(date,LocalTime.MAX);
            //获取当前天范围内的新增人员
            //select count(id) from user where create_time > begin and create_time < end
            Map map =new HashMap();
            map.put("begin",beginTime);
            map.put("end",endTime);
            Integer userCount =userMapper.getUserCount(map);
            if (userCount ==null){
                userCount=0;
            }
            a=a+userCount;
            userList.add(a);
            newUserList.add(userCount);
        }
        return UserReportVO.builder()
                .dateList(StringUtils.join(dateList,","))
                .totalUserList(StringUtils.join(userList,","))
                .newUserList(StringUtils.join(newUserList,","))
                .build();
    }

    /**
     * 订单统计
     * @param begin
     * @param end
     * @return
     */
    @Override
    public OrderReportVO getOrderStatistics(LocalDate begin, LocalDate end) {
        //连续存储时间
        List<LocalDate> dateList =new ArrayList<>();
        dateList.add(begin);
        while(!begin.equals(end)){
            begin=begin.plusDays(1);
            dateList.add(begin);
        }
        //搜索所有订单数和订单完成数
        Integer orderSum =0;
        Integer orderCompleteSum=0;
        /*//select count(id) from orders where create_time<end
        Map orderMap =new HashMap<>();
        orderMap.put("end",LocalDateTime.of(end,LocalTime.MAX));
        Integer orderSum=orderMapper.getOrderSumByMap(orderMap);
        orderMap.put("status",Orders.COMPLETED);
        Integer orderCompleteSum=orderMapper.getOrderSumByMap(orderMap);*/
        //搜索每天的订单数和订单完成数
        List<Integer> orderSumofDay =new ArrayList<>();
        List<Integer> orderCompleteSumofDay =new ArrayList<>();
        for (LocalDate date:dateList) {
            LocalDateTime beginTime=LocalDateTime.of(date,LocalTime.MIN);
            LocalDateTime endTime=LocalDateTime.of(date,LocalTime.MAX);
            Map map=new HashMap<>();
            map.put("begin",beginTime);
            map.put("end",endTime);
            Integer orderCount =orderMapper.getOrderSumByMap(map);
            orderSum +=orderCount;
            orderSumofDay.add(orderCount);
            map.put("status",Orders.COMPLETED);
            Integer validOrderCount =orderMapper.getOrderSumByMap(map);
            orderCompleteSum +=validOrderCount;
            orderCompleteSumofDay.add(validOrderCount);
        }
        //计算订单完成率
        Double orderCompletionRate = 0.0;
        if (orderSum!=0){
            orderCompletionRate = (double)orderCompleteSum/orderSum;
        }
        return OrderReportVO.builder()
                .dateList(StringUtils.join(dateList,","))
                .orderCountList(StringUtils.join(orderSumofDay,","))
                .validOrderCountList(StringUtils.join(orderCompleteSumofDay,","))
                .totalOrderCount(orderSum)
                .validOrderCount(orderCompleteSum)
                .orderCompletionRate(orderCompletionRate)
                .build();
    }

    /**
     * 统计菜品top10
     * @param begin
     * @param end
     * @return
     */
    @Override
    public SalesTop10ReportVO getSaleTop10(LocalDate begin, LocalDate end) {
        //获取指定时间
        LocalDateTime beginTime =LocalDateTime.of(begin,LocalTime.MIN);
        LocalDateTime endTime =LocalDateTime.of(end,LocalTime.MAX);

        //获取前十菜品
        List<GoodsSalesDTO> saleTop10 =orderMapper.getSaleTop10(beginTime,endTime);

        //使用stream流来保存nameList和numberList
        List<String> names =saleTop10.stream().map(GoodsSalesDTO::getName).collect(Collectors.toList());
        List<Integer> numbers =saleTop10.stream().map(GoodsSalesDTO::getNumber).collect(Collectors.toList());
        String nameList = StringUtils.join(names, ",");
        String numberList = StringUtils.join(numbers, ",");

        return SalesTop10ReportVO.builder()
                .nameList(nameList)
                .numberList(numberList)
                .build();
    }
}
